#读取列表页excel的url
#打开浏览器
#爬取信息
#关闭浏览器
import xlrd
from selenium import webdriver
import time
from xlwt import *
from selenium.common.exceptions import NoSuchElementException

file=xlrd.open_workbook('./house.xls')
# print(file.sheet_names())
table=file.sheets()[0]
nrows=table.nrows

browser=webdriver.Chrome()

excel=Workbook(encoding='utf-8')
sheet=excel.add_sheet('测试房源采集')

for i in range(nrows):
    #print(table.row_values(i))
    url=table.row_values(i)[0]
    print(i,url)
    browser.get(url)
    time.sleep(0.01)
    try:
        #price=browser.find_element_by_xpath('/html/body/section[2]/div/b').text
        price=browser.find_element_by_xpath('/html/body/section[3]/div/span/b').text
        village = browser.find_element_by_xpath('//*[@id="house-detail"]/div[2]/div[2]').text
        address = browser.find_element_by_xpath('//*[@id="house-detail"]/div[3]/div[2]').text
        tel = browser.find_element_by_xpath('//*[@id="house-detail"]/div[7]/div[2]/a').text
        linkman = browser.find_element_by_xpath('//*[@id="house-detail"]/div[6]/div[2]/span').text
        housetype = browser.find_element_by_xpath('//*[@id="house-detail"]/div[1]/div[2]').text
        decorate = browser.find_element_by_xpath('//*[@id="house-detail"]/div[4]/div[2]').text
        pubtime = browser.find_element_by_xpath('/html/body/div[2]').text
    except NoSuchElementException as msg:
        continue
    print(price,village,address,tel,linkman,housetype,decorate,pubtime)
    sheet.write(i, 0, price)
    sheet.write(i, 1, village)
    sheet.write(i, 2, address)
    sheet.write(i, 3, tel)
    sheet.write(i, 4, linkman)
    sheet.write(i, 5, housetype)
    sheet.write(i, 6, decorate)
    sheet.write(i, 7, pubtime)
    excel.save('./房源信息.xls')
browser.quit()
